package com.ihg.hiex.admin.visitor.impl;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ihg.hiex.admin.service.GenericDataService;
import com.ihg.hiex.admin.service.account.AccountService;
import com.ihg.hiex.admin.service.account.AdministratorService;
import com.ihg.hiex.admin.service.account.AssessorService;
import com.ihg.hiex.admin.service.account.DelegateService;
import com.ihg.hiex.admin.service.session.FinalTestService;
import com.ihg.hiex.admin.service.session.KnowingRelationService;
import com.ihg.hiex.admin.service.session.SessionDuringService;
import com.ihg.hiex.admin.service.session.SessionParticipatorService;
import com.ihg.hiex.admin.service.session.SessionService;
import com.ihg.hiex.admin.service.session.SessionTaskService;
import com.ihg.hiex.entity.session.Session;
import com.ihg.hiex.entity.session.before.finaltest.FinalTest;
import com.ihg.hiex.entity.session.during.SessionCalibration;
import com.ihg.hiex.entity.session.participator.SessionParticipator;
import com.ihg.hiex.entity.session.task.SessionTask;
import com.ihg.hiex.entity.user.Account;
import com.ihg.hiex.entity.user.Administrator;
import com.ihg.hiex.entity.user.Assessor;
import com.ihg.hiex.entity.user.Delegate;
import com.ihg.hiex.entity.user.KnowingRelation;
import com.ihg.hiex.entity.user.Role;


/**
 * @author Ysy
 *
 */
@Component("persistentVisitor")
public class PersistentVisitor {
    
    @Autowired
    private AdministratorService administratorService;
    
    @Autowired
    private SessionService sessionService;
    
    @Autowired
    private KnowingRelationService knowingRelationService;

    @Autowired
    private AssessorService assessorService;

    @Autowired
    private DelegateService delegateService;

    @Autowired
    private GenericDataService genericDataService;
    
    @Autowired
    private FinalTestService finalTestService;
    
    @Autowired
    private SessionTaskService sessionTaskService;
    
    @Autowired
    private SessionDuringService sessionDuringService;
    
    @Autowired
    private AccountService accountService;
    
    @Autowired
    private SessionParticipatorService sessionParticipatorService;
    
    /**
     * 
     * @return
     */
    public List<Administrator> getAdministrators() {
        return administratorService.list(Role.ADMIN.toString());
    }
    
    /**
     * 
     * @return
     */
    public List<Administrator> getCoaches() {
        return administratorService.list(Role.COACH.toString());
    }
    
    /**
     * 
     * @param adminId
     * @return
     */
    public List<Session> session(Long adminId) {
        Administrator administrator = administratorService.load(Administrator.class, adminId);
        return sessionService.list(administrator);
    }
    
    /**
     * @param assessorId
     * @return
     */
    public Assessor assessor(Long assessorId) {
        return assessorService.load(Assessor.class, assessorId);
    }
    
    /**
     * 
     * @param adminId
     * @return
     */
    public List<Delegate> delegate(Long adminId) {
        Administrator administrator = administratorService.load(Administrator.class, adminId);
        return sessionService.listDelegate(administrator);
    }
    
    /**
     * 
     * @param accountId
     * @return
     */
    public Account account(String accountId) {
        Long id = Long.parseLong(accountId);
        return accountService.load(Account.class, id);
    }
    
    /**
     * 
     * @param adminId
     * @return
     */
    public Administrator admin(String adminId) {
        Long id = Long.parseLong(adminId);
        return administratorService.load(Administrator.class, id);
    }
    
    /**
     * @param session
     * @param assessorId
     * @param delegateId
     * @return
     */
    public KnowingRelation getKnowingRelation(Session session, Long assessorId, Long delegateId) {
        
        if (assessorId != null && delegateId != null) {
            Assessor assessor = assessorService.load(Assessor.class, assessorId);
            Delegate delegate = delegateService.load(Delegate.class, delegateId);
            return knowingRelationService.load(session, assessor, delegate);
        }
        
        return null;
        
    }
    
    /**
     * @param session
     * @param assessor
     * @param parent
     * @return
     */
    public List<SessionTask> getSessionTasks(Session session, Assessor assessor, String parent) {
        return sessionService.listChildSessionTask(session, assessor, parent);
    }
    
    /**
     * @param id
     * @return
     */
    public Administrator getSessionHost(Long id) {
        return genericDataService.load(Administrator.class, id);
    }
    
    /**
     * @param assessor
     * @return
     */
    public List<FinalTest> listFinalTest(Assessor assessor) {
        return finalTestService.list(assessor);
    }
    
    /**
     * 
     * @param sessionId
     * @param delegateId
     * @param parent
     * @return
     */
    public List<SessionTask> delegateSessionTask(Long sessionId, Long delegateId, String parent) {
        Session session = sessionService.load(sessionId);
        Delegate delegate = delegateService.load(Delegate.class, delegateId);
        return sessionTaskService.listItemTaskByDelegate(session, delegate, parent);
    }
    
    /**
     * 
     * @param sessionId
     * @param assessorId
     * @param delegateId
     * @param parent
     * @return
     */
    public List<SessionTask> assessorSessionTask(Long sessionId, Long assessorId, Long delegateId, String parent) {
        Session session = sessionService.load(sessionId);
        Assessor assessor = assessorService.load(Assessor.class, assessorId);
        Delegate delegate = delegateService.load(Delegate.class, delegateId);
        return sessionTaskService.listItemTaskByAssessor(session, assessor, delegate, parent);
    }
    
    /**
     * 
     * @param sessionId
     * @param delegateId
     * @return
     */
    public SessionCalibration sessionCalibration(Long sessionId, Long delegateId) {
        Session session = sessionService.load(sessionId);
        Delegate delegate = delegateService.load(Delegate.class, delegateId);
        return sessionDuringService.loadSessionCalibration(session, delegate);
    }
    
    /**
     * 
     * @param delegateId
     * @return
     */
    public Session sessionByDelegate(Long delegateId) {
        SessionParticipator sessionParticipator = sessionParticipatorService.loadByDelegate(delegateId);
        return sessionParticipator.getSession();
    }
    
}
